package com.baike.controller;

import com.baike.data.Result;
import com.baike.domain.ProgressRequestDTO;
import com.baike.domain.VideoLearnedStatusDTO;
import com.baike.em.ConstantEnum;
import com.baike.util.IdWorker;
import com.baike.vo.VideoLearnVO;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.client.result.UpdateResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author:pjm
 */
@RestController
@RequestMapping("historyController")
@Api(value = "视频播放进度服务管理")
@RefreshScope //开启自动刷新配置
public class VideoLeranController {

    @Autowired
    private MongoTemplate mongoTemplate;
    //视频播放进度表增加数据
    @ApiOperation(value = "视频播放进度增加接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name="lessonId",value = "课程id",required = true,paramType = "query"),
            @ApiImplicitParam(name="studentId",value = "(学生)id",required = true,paramType = "query"),
            @ApiImplicitParam(name="incrementTime",value = "增量视频",required = true,paramType = "query"),
            @ApiImplicitParam(name="videoDuration",value = "视频总时长",required = true,paramType = "query"),
            @ApiImplicitParam(name="videoProgress",value = "视频播放进度",required = true,paramType = "query"),
            @ApiImplicitParam(name="learnedStatus",value = "学习状态",required = true,paramType = "query"),
            @ApiImplicitParam(name="createDate",value = "创建时间",required = true,paramType = "query"),
            @ApiImplicitParam(name="updateDate",value = "修改时间",required = true,paramType = "query"),
    })
    @PostMapping("/addVideoLearn")
    public Result addLearn(VideoLearnedStatusDTO videoLearned){
        IdWorker idWorker = new IdWorker();
        videoLearned.setVideoId(idWorker.nextId());
        VideoLearnedStatusDTO videoLearn = mongoTemplate.save(videoLearned);

        return Result.build(ConstantEnum.INSERT_SUCCESS).setResult(videoLearn);

    }

    //查询视频播放进度,根据前台传过来的参数
    @ApiOperation(value = "视频播放进度查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name="videoId",value = "视频id",required = true,paramType = "query"),
            @ApiImplicitParam(name="lessonId",value = "课程id",required = true,paramType = "query"),
            @ApiImplicitParam(name="studentId",value = "(学生)id",required = true,paramType = "query"),
    })
    @GetMapping("findAll")
    public Result<VideoLearnVO> findAll(){
        List<VideoLearnVO> list = mongoTemplate.findAll(VideoLearnVO.class);
        return Result.build(ConstantEnum.SUCCESS).setResult(list);

    }

    //接收前台所传参数,进行数据增加修改
    @ApiOperation(value = "视频播放进度修改接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name="progressRequestDTO",value = "学生(前台所传参数实体)",required = true,paramType = "body"),
    })
    @PutMapping
    public Result updateVideoLearn(@RequestBody ProgressRequestDTO progressRequestDTO){
        //先查出mongodb数据库文档的数据
        Query query = new Query();
        query.addCriteria(Criteria.where("videoId").is(progressRequestDTO.getVideoId()));
        query.addCriteria(Criteria.where("studentId").is(progressRequestDTO.getStudentId()));

        List<VideoLearnedStatusDTO> videoDTOS = mongoTemplate.find(query, VideoLearnedStatusDTO.class);
        if (videoDTOS.size() <= 0){//第一次观看
            VideoLearnedStatusDTO video = new VideoLearnedStatusDTO();
            BeanUtils.copyProperties(progressRequestDTO,video);
            Long videoDuration = video.getVideoDuration();//视频总时长
            Long videoProgress = video.getVideoProgress();//视频播放进度
            Long learnedDuration = video.getLearnedDuration();//第一次已学习时长
            //if(videoDuration * 2 / 3 < videoProgress){//判断学习时长是否是总时间2/3
            if(videoDuration * 2 / 3 < learnedDuration){//判断学习时长是否是总时间2/3
                video.setLearnedStatus(1);
            }else{
                video.setLearnedStatus(2);
            }
            video.setLearnedDuration(progressRequestDTO.
                    getIncrementTime()).
                    setCreateDate(new Date()).
                    setUpdateDate(new Date());;//第一次观看说明里面没有播放时,就直接将本次播放时间存入
            mongoTemplate.save(video);
        }else{//有观看史
            VideoLearnedStatusDTO videoLearnedStatusDTO = videoDTOS.get(0);
            Long videoDuration = videoLearnedStatusDTO.getVideoDuration();//视频总时长
            Long videoProgress = videoLearnedStatusDTO.getVideoProgress();//视频播放进度

            Long learnedDuration = videoLearnedStatusDTO.getLearnedDuration();//mongodb数据库信息
            Long incrementTime = progressRequestDTO.getIncrementTime();//前台传入增量时间
            Long completeLearn = learnedDuration + incrementTime; //已学习时间,第一次学习时长加第二次前台所传增量时间
            Update update = new Update();
 //           if(videoDuration * 2 / 3 < videoProgress){//判断播放进度是否是总时间2/3
            if(videoDuration * 2 / 3 < completeLearn){//判断已学习时长是否是总时间2/3
                update.set("learnedStatus",1);
            }else{
                update.set("learnedStatus",2);
            }

            update.set("updateDate",new Date());
            //update.set("learnedDuration",learnedDuration + incrementTime);
            update.set("learnedDuration",completeLearn);
            update.set("videoProgress",progressRequestDTO.getVideoProgress());
            UpdateResult updateResult = mongoTemplate.updateFirst(query, update, VideoLearnedStatusDTO.class);
        }
        return Result.build(ConstantEnum.UPDATE_SUCCESS);
    }


}
